package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.app.service.SectionService;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Maintain;
import com.zhongkai.model3.Section;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class SectionServiceImpl implements SectionService {

    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private PageQuerDao pageQuerDao;
    /**
     * 部门添加
     *
     * @param name      部门名称
     * @param plane     部门电话
     * @param principal 部门负责人
     * @param phone     负责人电话
     */
    @Override
    public ZhongkaiResult addSection(String name, String plane, String principal, String phone) {
        if (StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(plane) || StringUtils.isNullOrEmpty(principal)
                || StringUtils.isNullOrEmpty(phone)
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }
        Section section;
        section = (Section) iBaseDao.get("from Section se where se.secName='" + name + "' or se.secPlane='" + plane + "'");

        if (section != null) {
            return ZhongkaiResult.build(400, "部门或部门电话已存在");
        }




        section = new Section();

        section.setSecName(name);
        section.setSecPlane(plane);
        section.setSecPrincipal(principal);
        section.setSecPhone(phone);

        long millis = new DateTime().getMillis(); //获取当前系统时间戳
        section.setSecTime(millis);
        section.setSecRemove("1");

        try {
            iBaseDao.save(section);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "添加失败");
        }

    }


    /**
     * 部门修改
     *
     * @param name      部门名称
     * @param plane     部门电话
     * @param principal 部门负责人
     * @param phone     负责人电话
     */

    @Override
    public ZhongkaiResult updateSection(Integer id, String name, String plane, String principal, String phone) {
        if (StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(plane) || StringUtils.isNullOrEmpty(principal)
                || StringUtils.isNullOrEmpty(phone) || id == 0
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }
        Section section;
        section = (Section) iBaseDao.get("from Section se where se.id='" + id + "'");

        if (section == null) {
            return  ZhongkaiResult.build(400,"该部门不存在");
        }

        section.setSecName(name);
        section.setSecPlane(plane);
        section.setSecPrincipal(principal);
        section.setSecPhone(phone);

        try {
            iBaseDao.update(section);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "修改失败");
        }
    }

    @Override
    public ZhongkaiResult removeSection(Integer id) {
        if (id == 0){
            return ZhongkaiResult.build(400, "数据不完整");
        }else if (id == 1){
            return ZhongkaiResult.build(400, "维修部暂时不可删除");
        }

        Maintain maintain = (Maintain) iBaseDao.get("from Maintain ma where ma.tainSection='"+id+"' and ma.tainRemove='1'");

        if (maintain != null) {
            return ZhongkaiResult.build(400, "请先删除该部门人员");
        }
        Section section;
        section = (Section) iBaseDao.get("from Section se where se.id='" + id + "'");
        if (section == null) {
            return  ZhongkaiResult.build(400,"该部门不存在");
        }

        section.setSecRemove("2");

        try {
            iBaseDao.update(section);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "删除失败");
        }
    }

    @Override
    public ZhongkaiResult listSection(Integer page, Integer rows) {

        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Section.class);
            detachedCriteria.add(Restrictions.eq("secRemove","1")); //增加条件
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }
}
